package infos

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/app/sysparam"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/infod"

	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
)

type reqFeedBack struct {
	Kind    string `json:"kind" binding:"required"`                     // 类型
	Content string `json:"content,omitempty" binding:"required,min=10"` // 建议内容
}

func (c infoCtrl) postFeedBack(ctx *gin.Context) {
	var req reqFeedBack

	if err := ctx.BindJSON(&req); err != nil {
		c.ErrBadRequestByBind(ctx, 1, err)
		return
	}
	dctx := c.DDDContext(ctx)
	uid := dctx.UID()
	if uid == "" {
		c.ErrUnauthorized(ctx, "保存失败：未登录")
		return
	}
	desc := infod.InfoDesc{
		Kind:     infod.KindSimplify,
		Content:  req.Content,
		Tags:     []string{req.Kind},
		ColumnID: sysparam.FeedbackColumnid(),
	}
	_, err := c.iar.AddInfo(dctx, desc)
	if err != nil {
		c.ErrServerError(ctx, 6, "保存失败", errors.Wrap(err, "新增建议反馈失败："))
		return
	}
	c.Success(ctx, nil)
}
